class Solution {
public:
    int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
        int ret=0;
        int cnt=0;
        //tops[i]==tops[0]
        for(int i=1;i<tops.size();i++)
        {
            if(tops[i]==tops[0])
                continue;
            if(tops[i]!=tops[0] && bottoms[i]==tops[0])
            {
                cnt++;
            }
            else{
                cnt=INT_MAX;
                break;
            }
        }
        ret=cnt;
        //tops[i]==bottoms[0];
        cnt=0;
        for(int i=0;i<tops.size();i++)
        {
            if(tops[i]==bottoms[0])
                continue;
            if(tops[i]!=bottoms[0] && bottoms[i]==bottoms[0])
            {
                cnt++;
            }
            else{
                cnt=INT_MAX;
                break;
            }
        }
        ret=min(cnt,ret);
        cnt=0;
        //bottoms[i]==bottoms[0]
        for(int i=1;i<bottoms.size();i++)
        {
            if(bottoms[i]==bottoms[0])
                continue;
            if(bottoms[i]!=bottoms[0] && bottoms[0]==tops[i])
            {
                cnt++;
            }
            else{
                cnt=INT_MAX;
                break;
            }            
        }
        ret=min(cnt,ret);

        //bottoms[i]==tops[0]
        cnt=0;
        for(int i=0;i<bottoms.size();i++)
        {
            if(bottoms[i]==tops[0])
                continue;
            if(bottoms[i]!=tops[0] && tops[0]==tops[i])
            {
                cnt++;
            }
            else{
                cnt=INT_MAX;
                break;
            }            
        }
        ret=min(cnt,ret);
        return (ret==INT_MAX?-1:ret);
    }
};
